package top.anydata.lifeofcode.lifesystem.log;

import okhttp3.*;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * 功能描述:
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2024/4/18 4:49 PM
 */
public class OnlineAdapterLogReport {

    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");


    private static RequestBody gzip(final RequestBody body) {
        return new RequestBody() {
            @Override
            public MediaType contentType() {
                return body.contentType();
            }

            @Override
            public long contentLength() {
                return -1; // 无法提前知道压缩后的数据大小
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
                body.writeTo(gzipSink);
                gzipSink.close();
            }
        };
    }

    public static void main(String[] args) {

        OkHttpClient client = new OkHttpClient();

        String json = "[{\"liveMode\":3001,\"tid\":\"E5BD7FEF-6ADE-42E8-B579-8F8481F3C425\",\"sip\":\"\",\"cpu\":25.632301330566406,\"cip\":\"\",\"lip\":\"192.168.3.6\",\"ts\":1713411119363,\"agent\":\"OmniRtc_iOS_V3.29.46\",\"serv\":320,\"dev\":\"iPad7,11\",\"os\":\"14.4.1\",\"mem\":42.754631042480469,\"busGuid\":\"72D1B744-5B56-43BA-A271-2732DAB1DE8B\",\"net\":5,\"arch\":\"ARM64_V8\",\"ram\":3071600,\"engineid\":7,\"psId\":\"\",\"pri\":312,\"pridata\":{\"delay\":0,\"appMem\":0.42761403322219849,\"plan\":\"1003600851\",\"volume\":0,\"videoPermission\":\"\",\"roomStr\":\"1111\",\"vRtt\":3,\"audioBps\":0,\"audioLostRate\":0,\"videoFps\":0,\"sessionId\":\"bcd0ce6841394b7db456b22b73941705\",\"videoLostRate\":0,\"videoHeight\":0,\"audioEncodeFps\":0,\"videoEncodeFps\":0,\"videoBps\":0,\"audioPermission\":\"\",\"ver\":\"3.29.46\",\"uid\":11111,\"gatewayRtt\":3,\"aRtt\":3,\"videoWidth\":0,\"engine\":\"CoreRTC_iOS_V3.9.134\",\"videoTargetFps\":0,\"room\":\"15908740\",\"appcpu\":13,\"videoTargetBps\":0,\"streamId\":\"\"},\"appId\":\"gaoweigong\"}]"; // 你的JSON内容

        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url("https://rtc-logs.magic-school.com/stream.gif&appid=guoxiaoming")
                .addHeader("Content-Encoding", "gzip")
                .addHeader("logversion", "2.0.1")
                .addHeader("localtimestamp", System.currentTimeMillis() + "")
                .addHeader("projectid", "7")
                .addHeader("plan", "1231231")
                .addHeader("app", "gaoweigong")
                .post(gzip(body)) // 开启Gzip压缩
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                call.cancel();
                System.exit(0);  // 结束JVM
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("线上响应结果"+response.body().byteString());
                response.close();
                call.cancel();
                System.exit(0);  // 结束JVM
            }
        });

    }

}
